package com.example.util;

import java.io.File;
import java.io.IOException;

import android.util.Log;

import com.example.huashangapp.HSApplication;

public class ConfigCacheUtil {
	private static final String TAG = ConfigCacheUtil.class.getName();

	 

    public static final int CONFIG_CACHE_MOBILE_TIMEOUT  = 3600000;  //1 hour

    public static final int CONFIG_CACHE_WIFI_TIMEOUT    = 300000;   //5 minute

 

    public static String getUrlCache(String url) {

        if (url == null) {

            return null;

        }

 

        String result = null;

        File file = new File(HSApplication.mSdcardDataDir + "/" + getCacheDecodeString(url));

        if (file.exists() && file.isFile()) {

            long expiredTime = System.currentTimeMillis() - file.lastModified();

            Log.d(TAG, file.getAbsolutePath() + " expiredTime:" + expiredTime/60000 + "min");

            //1. in case the system time is incorrect (the time is turn back long ago)

            //2. when the network is invalid, you can only read the cache

            if (HSApplication.netWorkType !="" && expiredTime < 0) {

                return null;

            }

            if(HSApplication.netWorkType== "wifi"

                   && expiredTime > CONFIG_CACHE_WIFI_TIMEOUT) {

                return null;

            } else if (HSApplication.netWorkType =="mobile"

                   && expiredTime > CONFIG_CACHE_MOBILE_TIMEOUT) {

                return null;

            }

        }

        return result;

    }

 

    public static void setUrlCache(String data, String url) {

        File file = new File(HSApplication.mSdcardDataDir + "/" + getCacheDecodeString(url));

    }

 

    public static String getCacheDecodeString(String url) {

        //1. 处理特殊字符

        //2. 去除后缀名带来的文件浏览器的视图凌乱(特别是图片更需要如此类似处理，否则有的手机打开图库，全是我们的缓存图片)

        if (url != null) {

            return url.replaceAll("[.:/,%?&=]", "+").replaceAll("[+]+", "+");

        }

        return null;

    }
    /*  void getConfig(){

        //首先尝试读取缓存

        String cacheConfigString = ConfigCacheUtil.getUrlCache(CONFIG_URL);

        //根据结果判定是读取缓存，还是重新读取

        if (cacheConfigString != null) {

            showConfig(cacheConfigString);

        } else {

            //如果缓存结果是空，说明需要重新加载

            //缓存为空的原因可能是1.无缓存;2. 缓存过期;3.读取缓存出错

         AsyncHttpClient client = new AsyncHttpClient();

            client.get(CONFIG_URL, new AsyncHttpResponseHandler(){

 

                @Override

                public void onSuccess(String s){

                    //成功下载，则保存到本地作为后面缓存文件

                    ConfigCache.setUrlCache(result,  CONFIG_URL);

                    //后面可以是UI更新，仅供参考

                    showConfig(result);

                }

 

                @Override

                public void onFailure(Throwable arg0) {

                    //根据失败原因，考虑是显示加载失败，还是再读取缓存

                }

 

            });

        }
        }*/
}
